package cn.openea.perpartner.meily.rabbit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.openea.perpartner.meily.rabbit.domain.BusinessDto;
import cn.openea.perpartner.meily.rabbit.domain.DelayDto;
import cn.openea.perpartner.meily.rabbit.domain.GroupDto;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-02-07 16:23
 */
@Slf4j
@Service
public class PublisherService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 延时队列交换机
     */
    @Value("${rabbit.delay.exchange}")
    private String DELAY_EXCHANGE;
    /**
     * 延时队列路由键
     */
    @Value("${rabbit.delay.routing_key}")
    private String DELAY_ROUTE_KEY;
    /**
     * 业务队列路由键
     */
    @Value("${rabbit.business.routing_key}")
    private String BUSINESS_ROUTE_KEY;

    /**
     * 延时队列交换机---团购
     */
    @Value("${rabbit.group.exchange}")
    private String GROUP_EXCHANGE;

    /**
     * 队列路由键---团购
     */
    @Value("${rabbit.group.routing_key}")
    private String GROUP_ROUTE_KEY;

    /**
     * 发送延时消息
     * ttl 延时时间,单位毫秒
     */
    public boolean execute(DelayDto delayDto) {
        if (BeanUtil.isEmpty(delayDto)) {
            return false;
        }
        long ttl = delayDto.getDelayTime();
        if (ttl <= 0) {
            return false;
        }
        JSONObject object = (JSONObject) JSON.toJSON(delayDto);
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE, DELAY_ROUTE_KEY, object, message -> {
            message.getMessageProperties().setHeader("x-delay", ttl);
            return message;
        });
        log.info("当前时间:{},消息发送成功！延时周期时间{}毫秒,消息内容为{}", DateUtil.now(), ttl, delayDto);
        return true;
    }

    /**
     * 发送业务消息
     * @param dto
     */
    public boolean execute(BusinessDto dto) {
        if (BeanUtil.isEmpty(dto)) {
            return false;
        }
        long ttl = dto.getDelayTime();
        if (ttl <= 0) {
            return false;
        }
        JSONObject object = (JSONObject) JSON.toJSON(dto);
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE, BUSINESS_ROUTE_KEY, object, message -> {
            message.getMessageProperties().setHeader("x-delay", ttl);
            return message;
        });
        log.info("当前时间:{},业务消息发送成功！延时周期时间{}毫秒,消息内容为{}", DateUtil.now(), ttl, dto);
        return true;
    }

    /**
     * 发送消息---团购
     * @param dto
     */
    public boolean execute(GroupDto dto) {
        if (BeanUtil.isEmpty(dto)) {
            return false;
        }
        long ttl = dto.getDelayTime();
        if (ttl <= 0) {
            return false;
        }
        JSONObject object = (JSONObject) JSON.toJSON(dto);
        rabbitTemplate.convertAndSend(GROUP_EXCHANGE, GROUP_ROUTE_KEY, object, message -> {
            message.getMessageProperties().setHeader("x-delay", ttl);
            return message;
        });
        log.info("当前时间:{},业务消息【团购】发送成功！延时周期时间{}毫秒,消息内容为{}", DateUtil.now(), ttl, dto);
        return true;
    }
}
